<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Spieler zur Verfügung
 *
 * @author Robin
 */
include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_player.php";
include "../ff_includes/class_season.php";
include "../ff_includes/class_team.php";

$action = isset($_GET['action']) ? $_GET['action'] : "";

switch ($action) {
    case 'list':
        listPlayers();
        break;
    case 'new':
        newPlayer();
        break;
    case 'edit':
        editPlayer();
        break;
    case 'save':
        savePlayer();
        break;
    case 'delete':
        deletePlayer();
        break;
    default:
        listPlayers();
}

function listPlayers() {
    $db = new Database();
    $teamId = isset($_GET['teamId']) ? (int) $_GET['teamId'] : -1;
    $seasonId = isset($_GET['seasonId']) ? (int) $_GET['seasonId'] : -1;

    // Spielerliste anhand der gesetzten Filter-Parameter holen
    if ($teamId > 0) {
        if ($seasonId > 0) {
            $playerList = Player::getPlayersByTeamAndSeason($teamId, $seasonId);
        } else {
            $playerList = Player::getPlayersByTeam($teamId);
        }
    } else {
        if ($seasonId > 0) {
            $playerList = Player::getPlayersBySeason($seasonId);
        } else {
            $playerList = Player::getPlayers();
        }
    }
    $numbPlayers = $playerList['totalRows'];
    $players = $playerList['playerList'];

    // Liste der Saisons und Teams fuer die Filterauswahl
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];

    require("templates/player_list.php");
}

function newPlayer() {
    $db = new Database();
    $player = new Player();
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $numbSeasons = $seasonList['totalRows'];
    $playerSeasonList = [];

    require("templates/player_edit.php");
}

function editPlayer() {
    $db = new Database();
    $playerId = (int) $_GET['playerId'];
    $player = Player::getPlayerById($playerId);
    $playerSeasonList = Player::getSeasonsByPlayerId($playerId);

    // Liste der Saisons und Teams fuer die Zuweisung
    $seasonList = Season::getSeasonList();
    $seasons = $seasonList['seasonList'];
    $numbSeasons = $seasonList['totalRows'];
    $teamList = Team::getTeamList();
    $teams = $teamList['teamList'];

    require("templates/player_edit.php");
}

function savePlayer() {
    // speichern
    if (isset($_POST['saveChanges'])) {
        $playerArray['player_id'] = $_POST['player_id'];
        $playerArray['player_firstname'] = $_POST['player_firstname'];
        $playerArray['player_lastname'] = $_POST['player_lastname'];
        $playerArray['player_team'] = $_POST['player_team'];
        if (isset($_POST['player_status'])) {
            $playerArray['player_status'] = $_POST['player_status'];
        }
        else {
            $playerArray['player_status'] = 0;
        }
        
        $player = new Player($playerArray);
        
        $seasonIds = $_POST['player_seasons'];

        // update
        if ($player->getId() != 0) {
            $player->update();
            $player->setSeasons($seasonIds);
        }
        // neuer Eintrag
        else {
            $player->insert();
            $player->setSeasons($seasonIds);
        }
       header("Location: player.php?action=list");
    }
    // abbrechen
    elseif (isset($_POST['cancel'])) {
        header("Location: player.php?action=list");
    }
}

function deletePlayer() {
    $player = Player::getPlayerById((int) $_GET['playerId']);
    $player->setSeasons(array()); // leerer Array um den Spieler aus der Verknuepfungstabelle zu loeschen
    $player->delete();
    header("Location: player.php?action=list");
}

?>
